'use strict';

app.controller('RoleController', ['$cookieStore', 'infoRoleService', '$scope', '$http', '$state', '$window', 'MODE', 
	function($cookieStore, infoRoleService, $scope, $http, $state, $window, MODE) {
	
	$scope.$on("page.query", function(event, data) {
		infoRoleService.getCount($scope.queryParam).then(function(data){
			if (data.code=='1001') {
				$scope.$broadcast("page.query.count",data);
			}
		});
		$scope.setRoleList();
	});
	
	$scope.setRoleList = function(){
		console.log('infoRoleService',infoRoleService);
		var page = $cookieStore.get("page");
		infoRoleService.getRoleList(page, $scope.queryParam).then(function(data){
		console.log('data',data);
			if (data.code=='1001') {
					console.log("获取用户资料信息:", data );
					$scope.roleList = data.data;
					
			} else if(data.code == '10010010'){  //非法请求
				$state.go("app.user-role");
			};
		});
	};
	
	
	$scope.submit = function() {
		var role = $scope.formData;
		if($scope.mode == MODE.add) {
			infoRoleService.addRole(role).then(function(data){
				if (data.code=='1001') {
					$scope.$emit("page.query");
				} else{
					alert("插入失败");
				};
			});
		} else if($scope.mode == MODE.edit) {
			infoRoleService.updateRole(role).then(function(data){
				if (data.code=='1001') {
					$scope.setRoleList($scope.page.pageNo);
				} else{
					alert("更新失败");
				};
			});
		}

		$('#roleModal').modal('hide');
		$('#roleModal').on('hidden.bs.modal', function () {
		  	$scope.close();
		})
	}
	
	$scope.query = function() {
		$scope.setRoleList($scope.page.pageNo);
	}
	

	$scope.close = function(){
		
		$scope.formData = {};
		$scope.form = {};
	}
	
	$scope.setAddRole = function(){
		$scope.addRole = true;
		$scope.roleRights = false;
		$scope.allotRights = false;
		$scope.showAllotRights = false;
		$scope.mode = MODE.add;
		$scope.close();
	};
	
	$scope.edit = function(id){
		$scope.addRole = true;
		$scope.roleRights = false;
		$scope.allotRights = false;
		$scope.showAllotRights = false;
		$scope.mode = MODE.edit;
		$scope.roleList.forEach(function(role){
			if (role.id==id) {
				$scope.formData = role;
			}
		})
	};
	$scope.allotRoleRights = function(roleId){
		$scope.addRole = false;
		$scope.roleRights = true;
		$scope.allotRights = false;
		$scope.showAllotRights = true;
		$scope.roleId = roleId;
		$scope.$broadcast("setRoleAuthList",roleId);
		$scope.roleList.forEach(function(auth){
			if (auth.id==roleId) {
				$scope.roleAuth = auth;
				$scope.roleAuth.desc = "";
			}
		});
		
	};
	$scope.setAllotRights = function(authId){
		$scope.addRole = false;
		$scope.roleRights = false;
		$scope.allotRights = true;
		$scope.showAllotRights = true;
		$scope.$broadcast("setRoleAuth",authId);
	};
	$scope.remove = function(id){
		if($window.confirm("确定要删除该条信息?")) {
			infoRoleService.deleteById(id).then(function(data){
				if (data.code=='1001') {
					$scope.$emit("page.query");
					$scope.setRoleList($scope.page.pageNo);
				} else{
					alert('删除失败');
				};
				
			});
		}
	};
}]);

app.controller('infoRoleAuthCtl',function($cookieStore, $state, $scope, $window,InfoRoleAuthService,infoAuthService,MODE){
	
	$scope.$on("setRoleAuth",function(event,authId){
		$scope.authList.forEach(function(auth){
			if (auth.id==authId) {
				$scope.roleAuth.authId = auth.id;
				$scope.roleAuth.roleId = $scope.roleId;
			}
		});
	});
	
	$scope.$on("setRoleAuthList", function(event, roleId) {
		$scope.setAuthList(roleId);
	});
	
	$scope.setAuthList = function(roleId){
		var page = $cookieStore.get("pages");
		infoAuthService.getAuthList(page, $scope.queryParam).then(function(data){
			if (data.code=='1001') {
					console.log("获取用户资料信息:", data );
					$scope.authList = data.data;
					InfoRoleAuthService.getRoleAuth(roleId).then(function(data){
						$scope.roleAuthList = data.data;
						console.log("角色权限list:",$scope.roleAuthList);
						$scope.authList.forEach(function(auth) {
							auth.exit = false;
							$scope.roleAuthList.forEach(function(roleAuth) {
								if(auth.id == roleAuth.authId) {
									auth.exit = true;
									auth.roleAuthId = roleAuth.id;
								}
							});
						})
					});
					
			} else if(data.code == '10010010'){  //非法请求
				$state.go("app.user-role");
			};
		});
	};
	
	$scope.$on("page.querys", function(event, data) {
		infoAuthService.getCount($scope.queryParam).then(function(data){
			if (data.code=='1001') {
				$scope.$broadcast("page.query.counts",data);
			}
		});
		$scope.setAuthList($scope.roleId);
	});
	
	$scope.addRoleAuth = function(){
		var roleAuth = $scope.roleAuth;
		InfoRoleAuthService.addRoleAuth(roleAuth).then(function(data){
			if (data.code=='1001') {
					$scope.roleAuth.desc = "";
					$scope.$emit("page.querys");
					$scope.allotRoleRights(roleAuth.roleId);
				} else{
					alert("插入失败");
				};
		})
		
	}
	
	$scope.removeRoleAuth = function(roleAuthId) {
		if($window.confirm("确定要取消角色权限?")) {
			InfoRoleAuthService.deleteById(roleAuthId).then(function(data) {
				if(data.code == '1001') {
					$scope.$emit("page.querys");
					$scope.allotRoleRights($scope.roleId);
				} else {
					alert('删除失败');
				};

			});
		}
	};
	
});